package com.jgj.zxkj.lookandcheckjggroupinfo.login;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.LogUtils;
import com.jgj.zxkj.lookandcheckjggroupinfo.R;
import com.jgj.zxkj.lookandcheckjggroupinfo.TestDelegate;
import com.sa.all_cui.mix_core.delegate.DoggerDelegate;
import com.sa.all_cui.mix_core.net.RestClient;
import com.sa.all_cui.mix_core.net.callback.IError;
import com.sa.all_cui.mix_core.net.callback.ISuccess;

import butterknife.BindView;
import butterknife.OnClick;
import qiu.niorgai.StatusBarCompat;

/**
 * Created by all-cui on 2017/10/26.
 */

public class SignInDelegate extends DoggerDelegate {
    @BindView(R.id.et_sign_in_account)
    TextInputEditText mUserAcount;
    @BindView(R.id.et_sign_in_pwd)
    TextInputEditText mPwd;

    private ISignListener mSignListener;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof ISignListener) {
            mSignListener = (ISignListener) context;
        }
    }
    @OnClick(R.id.bt_sign_in)
    void clickSignInView() {
        getSupportDelegate().start(new TestDelegate());
        final String account = mUserAcount.getText().toString().trim();
        final String pwd = mPwd.getText().toString().trim();
        if (checkForm(account, pwd)) {
            RestClient.builder()
                    .url("login")
                    .params("loginId", account)
                    .params("password", pwd)
                    .loader(getContext())
                    .success(new ISuccess() {
                        @Override
                        public void onSuccess(String response) {
                            LogUtils.i(response);
                            SignHandler.onSignIn(response,mSignListener);
                        }
                    })
                    .error(new IError() {
                        @Override
                        public void onError(int code, String msg) {
                            LogUtils.i("lol-zhangyoubao", code + "\r\n" + msg);
                        }
                    })
                    .build()
                    .get();
        }
    }

    //检查表单的格式是否正确
    private boolean checkForm(String account, String pwd) {
        boolean isPass = true;
        if (TextUtils.isEmpty(account)) {
            mUserAcount.setError(getString(R.string.sign_up_tip_account));
            isPass = false;
        } else {
            mUserAcount.setError(null);
        }

        if (TextUtils.isEmpty(pwd) || pwd.length() < 6) {
            mPwd.setError(getString(R.string.sign_up_error_pwd));
            isPass = false;
        } else {
            mPwd.setError(null);
        }
        return isPass;
    }



    @Override
    public Object setLayout() {
        StatusBarCompat.translucentStatusBar(getProxyActivity(), true);
        return R.layout.delegate_sign_in;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View view) {

    }
}
